package com.weixin.engine.daily.aviator;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorLong;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.util.Map;

public class CustomFunction extends AbstractFunction {

    @Override
    public String getName() {
        return "customFunc";
    }

    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
        Number num1 = arg1.numberValue(env);
        Number num2 = arg2.numberValue(env);
        Long sum = num1.longValue() + num2.longValue();
        return AviatorLong.valueOf(sum);
    }

    public static void main(String[] args) {
        // 将自定义函数注册到Aviator中
        AviatorEvaluator.addFunction(new CustomFunction());
        // 执行
        Long result = (Long) AviatorEvaluator.execute("customFunc(50,20)");
        // 输出结果
        System.out.println(result);
    }
}
